import json

import requests

from basic.base import BaseReqest

class URL:
    _MEMBER_LIST = "adminapi/member/member"
    _MEMBER_ADD = "adminapi/member/member"
    _MEMBER_MODUFY = "adminapi/member/member/modify"
    _MEMBER_MASSAGE = "adminapi/member/member"
    _MEMBER_LABEL_ADD = "adminapi/member/label"
    _MEMBER_LABEL_MODUFY = "adminapi/member/label"
    _MEMBER_LABEL_LIST = "adminapi/member/label"

class Member(BaseReqest, URL):

    def member_list(self, keyword=None, register_type=None, label=None, register_channel=None, level=None, **kwargs):
        # """会员列表、支持条件查询"""
        """
        :param keyword: 手机、邮箱、姓名、昵称
        :param register_type: 注册来源
        :param label: 会员标签
        :param register_channel:
        :param level: 会员等级
        :return: response.json()
        """
        data = {
            'page': '1',
            'limit': '10',
            'keyword': keyword,
            'register_type': register_type,
            'member_label': label,
            'register_channel': register_channel,
            'member_level': level,
            **kwargs
        }
        return self._get(api_name=self.member_list.__name__, url=self._MEMBER_LIST, params=data)

    def member_add(self, nickname, member_no, mobile, password, **kwargs):
        """会员添加"""
        data = {
            "nickname": nickname,
            "member_no": member_no,
            "mobile": mobile,
            "password": password,
            "password_copy": password,
            **kwargs
        }
        return self._post(api_name=self.member_add.__name__, url=self._MEMBER_ADD, data=data)

    def member_modify(self, member_id, modify, value):
        """
        :param member_id:
        :param field: sex:性别 0:保密 1:男 2:女 / member_label:会员标签 / member_level:会员等级
        :return:
        """
        # """会员编辑"""
        M_URL = self._MEMBER_MODUFY + f"/{member_id}/{modify}"
        data = {"member_id": member_id, "field": modify, "value": value}
        return self._put(api_name=self.member_modify.__name__, url=M_URL, data=data)

    def member_massage(self, member_id):
        """会员信息"""
        M_URL = self._MEMBER_MASSAGE + f"/{member_id}"
        return self._get(api_name=self.member_massage.__name__, url=M_URL)

    def member_label_list(self, label_name):
        """标签列表"""
        data = {'page': '1', 'limit': '10', 'label_name': label_name}
        return self._get(api_name=self.member_label_list.__name__, url=self._MEMBER_LABEL_LIST, params=data)

    def member_label_add(self, name, **kwargs):
        """标签添加"""

        data = {
            "label_id": "",
            "label_name": name,
            **kwargs

        }
        return self._post(api_name=self.member_label_add.__name__, url=self._MEMBER_LABEL_ADD, data=data)

    def member_label_modify(self, label_id, label_name, intro, sort):
        """标签编辑"""
        data = {
            'method': 'put',
            'url': f'http://124.70.39.201/adminapi/member/label/{label_id}',
            'headers': {'token': self.token, 'Content-Type': 'application/json'},
            'json': {'label_id': label_id, 'label_name': label_name, 'memo': intro, 'sort': sort}
        }
        response = requests.request(**data)
        return self._put(url=response.url, data=data)

    def member_label_delete(self, label_id):
        """标签删除"""
        data = {
            "method": "DELETE",
            "url": f"http://124.70.39.201/adminapi/member/label/{label_id}",
            "headers": {"token": self.token}

        }
        r = requests.request(**data)
        return r.json()

    def member_level_list(self):
        """会员等级列表"""
        data = {
            "method": "GET",
            "url": "http://124.70.39.201/adminapi/member/level?page=1&limit=10&level_name=",
            "headers": {"token": self.token},
        }
        r = requests.request(**data)
        return r.json()

    def member_level_add(self):
        """会员等级添加"""
        pass

    def member_config_growth_rule(self):
        """会员成长值规则配置查询"""
        pass

    def member_account_balance(self):
        """账户余额"""
        pass

    def member_account_commission(self):
        """账户佣金"""
        pass

if __name__ == '__main__':
    member = Member()
    member.member_massage(215)